import time
import unittest
from autoframe.common.functions import open_browser
from autoframe.page.register_page import RegisterPage
from autoframe.page.index_page import IndexPage
import os
from autoframe.common.data_operation import DataOperation
import ddt
from autoframe.page.index_page import IndexPage

project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
path = os.path.join(project_path, 'data/registry.xlsx')
account_list1 = DataOperation(path, 'registry.xlsx', 0).read_data_return_dict()
account_list2 = DataOperation(path, 'registry.xlsx', 1).read_data_return_dict()


@ddt.ddt
class RegisterTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        # 打开浏览器
        driver = open_browser()
        # 创建对象
        cls.register = RegisterPage(driver)
        cls.index = IndexPage(driver)
        # 输入网址
        cls.register.get('http://172.16.140.57/ecshop/')

    @ddt.data(*account_list1)
    def test_01_login(self, data):
        # # 输入网址
        # self.register.get('http://172.16.140.57/ecshop/')
        # 点击免费注册
        self.register.click_register()
        # 输入用户名
        self.register.input_username(data['user'])
        # 输入邮箱
        self.register.input_email(data['email'])
        # 输入密码
        self.register.input_password1(data['password1'])
        # 确认密码
        self.register.input_password(data['password'])
        # 输入qq
        self.register.input_qq(data['qq'])
        # 输入办公电话
        self.register.input_office_telephone(data['telephone'])
        # 输入家庭电话
        self.register.input_home_phone(data['home_phone'])
        # 输入手机
        self.register.input_phone(data['phone'])
        # 选择密码提示问题
        self.register.select_password_prompt_question(data['question'])
        # 输入密码问题答案
        self.register.input_password_question_answer(data['answer'])
        # 点击立即注册
        self.register.register_now()
        time.sleep(1)
        # 断言注册成功
        username = self.index.font_text()
        self.assertEqual(data['user'], username)
        # 退出登录
        self.register.log_out()

    @ddt.data(*account_list2)
    def test_02_login(self, data):
        # 点击免费注册
        self.register.click_register()
        # 输入用户名
        self.register.input_username(data['user'])
        # 输入邮箱
        self.register.input_email(data['email'])
        # 输入密码
        self.register.input_password1(data['password1'])
        # 确认密码
        self.register.input_password(data['password'])
        # 输入qq
        self.register.input_qq(data['qq'])
        # 输入办公电话
        self.register.input_office_telephone(data['telephone'])
        # 输入家庭电话
        self.register.input_home_phone(data['home_phone'])
        # 输入手机
        self.register.input_phone(data['phone'])
        # 选择密码提示问题
        self.register.select_password_prompt_question(data['question'])
        # 输入密码问题答案
        self.register.input_password_question_answer(data['answer'])
        # 点击立即注册
        self.register.register_now()
        # 断言注册失败
        # username = self.index.font_text()
        # self.assertNotEqual(data['user'], username)

    @classmethod
    def tearDownClass(cls) -> None:
        # 关闭浏览器
        cls.register.quit()


if __name__ == '__main__':
    unittest.main(verbosity=2)
